import java.util.*;

public class L1282 {
    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        List<List<Integer>> res = new LinkedList<>();
        Map<Integer, List<Integer>> map = new HashMap<>();
        // 更新map，存储key元素->所有元素的下表
        for (int i = 0; i < groupSizes.length; i++) {
            List<Integer> list = map.getOrDefault(groupSizes[i], new ArrayList<>());
            list.add(i);
            map.put(groupSizes[i], list);
        }
        // 遍历map，每到key的数量添加res， 重新创建一个list，
        map.forEach((key, value)->{
            LinkedList<Integer> temp = new LinkedList<>();
            for (Integer index : value) {
                temp.add(index);
                if (temp.size() == key){
                    res.add(new LinkedList<>(temp));
                    temp.clear();
                }
            }
        });
        return res;
    }

    public static void main(String[] args) {
        List<List<Integer>> lists = new L1282().groupThePeople(new int[]{3, 3, 3, 3, 3, 1, 3});
        lists.forEach(p->{
            p.forEach(x->{
                System.out.print(x);
            });
            System.out.println();
        });
    }
}
